/*
 * @Author: 0x9DEFA478
 * @Date: 2022-07-24 17:34:14
 * @LastEditTime: 2022-07-27 18:18:35
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper65




void H_NES_Mapper65_PRG_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p[Address-0x6000U]=Value;
  }else{
    extern void H_NES_Mapper65_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console);
    H_NES_Mapper65_PRG_Write_Ext(Value,Address,Console);
  }
}
Hbyte H_NES_Mapper65_PRG_Read(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    return p[Address-0x6000U];
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];
    
    return p[Address&0x1FFFU];
  }
}
Huint16 H_NES_Mapper65_PRG_Read16(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint16 address;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    p=&p[Address-0x6000U];

    if(Address==0x7FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper65_PRG_Read(address,Console))<<8);
    }

    return ((Huint16)p[0])|(((Huint16)p[1])<<8);
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];
    
    address=Address&0x1FFFU;
    p=&p[address];

    if(address==0x1FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper65_PRG_Read(address,Console))<<8);
    }
    
    return ((Huint16)p[0])|(((Huint16)p[1])<<8);
  }
}




void H_NES_Mapper65_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;
  
  H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionCHRWriteAccess,Address);
}
void H_NES_Mapper65_CHR_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU]=Value;
}
Hbyte H_NES_Mapper65_CHR_Read(Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  return Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU];
}




void H_NES_Mapper65_CPUTick(int CPUCycle,void* v){
  H_NES_Console* Console;
  Huint16 Count;

  Console=v;

  if((Console->ThisMapper.Reg_9003&0x80U)!=0x00U){

    Count=Console->ThisMapper.Count;
    if(Count>0){
      if(Count>CPUCycle){
        Console->ThisMapper.Count=Count-CPUCycle;
      }else{
        Console->ThisMapper.Count=0U;
        Console->CPU.Status|=vH_NES_CPU_Status_IRQ_ExtIRQ;
      }
    }

  }

}




#undef ThisMapper



